package com.ts.enterprise.common.config;


import com.ts.enterprise.common.annotation.CurrentUser;
import com.ts.enterprise.common.consts.Constant;
import com.ts.enterprise.module.user.entity.User;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @Author Administrator
 * @Date 2021/10/19 11:05
 * @Description
 */
public class CurrentUserResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(CurrentUser.class) && parameter.getParameterType().equals(User.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return (User)webRequest.getAttribute(Constant.CURRENT_USER, RequestAttributes.SCOPE_REQUEST);
    }
}
